// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    info:'',
    name:'',
    id_card:'',
    serial_number:'',//序列号
    isChange:false
  },
  onChangeSub(){
    if(!this.data.isChange){
      this.onSubmit();
      return false
    }
    let that = this;
    wx.showLoading({
      title: '提交中',
      mask:true
    })
      wx.request({
        url: app.globalData.baseUrl +'/api/my/updateUserInfo',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'token':app.globalData.token,
        },
        data:{
          id_card:this.data.id_card,
          name:this.data.name
          // type:type
        },
        success: function(res) {  
          wx.hideLoading()   
          if(res.data.status){
            that.onSubmit()
           
          }else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
          console.log(res)
        },
        fail:function(){
          wx.hideLoading()   
        }
      })
  },
   //提交
   onSubmit(){
    let that = this;
    wx.showLoading({
      title: '提交中',
      mask:true
    })
      wx.request({
        url: app.globalData.baseUrl +'/api/my/active',
        method: 'GET',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'token':app.globalData.token,
        },
        data:{
          serial_number:that.data.serial_number,
          // type:type
        },
        success: function(res) {  
          wx.hideLoading()   
          if(res.data.status){
            wx.navigateTo({
              url: '/pages/activate/success/index',
            })
          }else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
          console.log(res)
        },
        fail:function(){
          wx.hideLoading()   
        }
      })
    
  },
  onChangeCard(e){
    this.setData({
      isChange:true
    })
    this.setData({
      id_card:e.detail.value
    })
  },
  onChangeName(e){
    this.setData({
      isChange:true
    })
    this.setData({
      name:e.detail.value
    })
  },
  onLoad(options) {
    let that = this;
    this.setData({
      serial_number:options.id
    })
    wx.request({
      url: app.globalData.baseUrl + '/api/my/userInfo',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token':app.globalData.token,
      },
      success: function(res) {   
        console.log(res)
        if(res.data.status){
          that.setData({
            name:res.data.data.records.get_driver_info.name,
            id_card:res.data.data.records.get_driver_info.id_card,
          })
        }              
        console.log(res)
      },
      fail: function() {
        console.log("请求失败")
      }
    })
  },
 
  
 
})
